Unique Path Problem
Unique path is classic problem solving by dynamic programming. There are many follow-ups for this problem. So I’m writting this blog trying to do some summary and have a better understanding of this kind of problem.
First simple unique path problem
This problem is from LeetCode 62.
A robot is located at the top-left corner of a m x n grid.
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Note: m and n will be at most 100.
1 | Input: m = 7, n = 3 |
First Solution:
1 | class Solution { |
Then I could optimsize the space complexity from O(mn) to O(n).
1 | class Solution { |
Unique Path II
This problem is from LeetCode 63.
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
Note: m and n will be at most 100.
1 | Input: |
Solution:
1 | class Solution { |
Then we also could optimsize the space complexity from O(mn) to O(n):
1 | class Solution { |
Unique Path III
A robot is located at the top-left corner of a m x n grid.
The robot can only move either right or upper right or bottom right at any point in time. The robot is trying to reach the bottom-right corner of the grid.
How many possible unique paths are there?
1 | Input: m = 3, n = 7 |
Solution:
Considering a grid could only come from upper left, bottom left or left, so we need to calculate the numbers for left first. So the traversal order should from left to right, then from top to bottom.
1 | class Solution { |
Then we could do some optimization on space complexity from O(mn) to O(m)
1 | class Solution { |
Follow up 1
Give three point, find if there is a path going through these three points.
1 | input: m = 3, n = 7, points = {{1,1}, {2,2}, {3,3}} |
Idea:
As we could only go towards right, so the most left point must be the first to o through. So we need to sort these three points first to know the traversal order.
The difference between column number should be bigger than row numbers. If this requirment isn’t satisfied, the result should be false;
Solution:
1 | class Solution { |
Follow up 2
Give three point, find the amount of path there is a path going through these three points.
Idea:
As we could only go towards right, so the most left point must be the first to o through. So we need to sort these three points first to know the traversal order.
And at the row number equals to one point, we only count the numbers of the target point, others will be kept 0.
1 | class Solution { |
Follow up 3
Give a lower bound x == H, find all path that go through the lower bound(x >= H)
1 | input: m = 3, n = 7, H = 2 |
Idea:
First use the normal method to do the traverse and calculate the number, then modify the number to 0 if X < H. Then we traverse from x == H, and calculate the answer.
Solution:
1 | class Solution { |
Follow up 4
A robot is located at the top-left corner of a m x n grid.
The robot can only move either down or bottom left or bottom right at any point in time. The robot is trying to reach the bottom-left corner of the grid.
How many possible unique paths are there?
1 | input: m = 3, n = 7 |
Idea:
It’s basicly the same idea just we need to calculate the top level first. So the traversal order should from top to bottom, then from left to right.
Solution:
1 | class Solution { |